/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import Bean.User;
import dao.interfacce.UserDAO;
import dao.interfacce.UserDAO_DB;
import exception.LoginException;

/**
 *
 * @author Vincenzo
 */
public class LoginManager {

    public UserDAO udao;

    public LoginManager(String connection, String username, String password)throws Exception{
        udao=new UserDAO_DB(connection, username, password);
    }

    private boolean loginCheck(String username, String password) throws LoginException{
        User w=null;
        if(udao.isWorker(username)){
            w=udao.getWorker(username);
            if(w.getPassword().equals(password))
                return true;
            else
                throw new LoginException("Uncorrect Password");
        }
        else{
            
            return false;
        }
    }

    public User getWorker(String username, String password) throws LoginException{
        User w=null;
        if(this.loginCheck(username, password)){
           w=udao.getWorker(username);
           return w;
        }
        else
            throw new LoginException("Uncorrect Username and Password");
    }

    private boolean registerCheck(String username) throws LoginException{
        if(udao.isWorker(username))
            return true;
        else
            return false;
    }

    public void register(String username, String password, String work, String name, String surname, String birthDate) throws LoginException{
        if(!this.registerCheck(username))
            udao.storeWorker(username, password, work, name, surname, birthDate);
        else
            throw new LoginException("Username and password already in use");
    }
}
